<template>
  <el-dialog title="关联分类" :visible.sync="open" width="800px" append-to-body>
    <el-cascader
      style="width: 50%"
      v-model="params.catelogId"
      :options="categorys"
      clearable
      :props="props"
      filterable
      size="small"
      collapse-tags
    ></el-cascader>

    <el-button type="primary" @click="handleAdd" size="small" style="margin-left: 10px">新增关联</el-button>

    <el-table v-loading="loading" :data="list" style="margin-top: 10px">
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="品牌名称" align="center" prop="brandName"/>
      <el-table-column label="分类名称" align="center" prop="catelogName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="params.pageNum"
      :limit.sync="params.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'relation',
  data() {
    return {
      open: false,
      loading: false,
      props: {
        label: 'name',
        value: 'catId',
        multiple: true
      },
      categorys: [],
      list: [],
      total: 0,
      params: {
        catelogId: undefined,
        brandId: undefined,
        pageNum: 1,
        pageSize: 10
      },
      uri: '/product/categoryBrandRelation'
    }
  },
  methods: {
    init(brandId) {
      this.params.catelogId = []
      this.params.brandId = brandId
      this.open = true
      this.params.pageNum = 1
      this.params.pageSize = 10
      this.getList()
      this.getCategorys()
    },
    getCategorys() {
      this.$api.get('/product/category/tree').then(({ data }) => {
        this.categorys = data.data
      })
    },
    getList() {
      this.loading = true
      this.$api.get(this.uri + '/category/list', this.params).then(({ data }) => {
        this.list = data.records
        this.total = data.total
        this.loading = false
      })
    },
    handleAdd() {
      // 得到的是一个二维数组
      let all = this.params.catelogId
      if (this.params.catelogId.length == 0) {
        this.$modal.msgError('请选择分类')
      } else {
        // 获取最后一项
        const ids = all.map(i => i[i.length - 1])
        const data = {
          brandId: this.params.brandId,
          catelogIdList: ids
        }
        this.$api.put(this.uri, data).then(resp => {
          this.params.catelogId = []
          this.getList()
          this.$modal.msgSuccess('新增成功')
        })
      }
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(() => {
        return this.$api.delete(this.uri + '/' + row.id)
      }).then((resp) => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      })
    }
  }
}
</script>

<style scoped>

</style>
